#include "VScintillaBase.h"
#include <QVariant>

VScintillaBase::VScintillaBase(QWidget * parent):
    QsciScintilla(parent)
{
}

int VScintillaBase::cursorPos()
{
    return this->SendScintilla(QsciScintilla::SCI_GETCURRENTPOS);
}

int VScintillaBase::cursorOfLine()
{
    return this->SendScintilla(QsciScintilla::SCI_LINEFROMPOSITION, this->cursorPos());
}

int VScintillaBase::anchorPos()
{
    return this->SendScintilla(QsciScintilla::SCI_GETANCHOR);
}

int VScintillaBase::anchorOfLine()
{
    return this->SendScintilla(QsciScintilla::SCI_LINEFROMPOSITION, this->anchorPos());
}

int VScintillaBase::posOfLine(int pos)
{
    return this->SendScintilla(QsciScintilla::SCI_LINEFROMPOSITION, pos);
}

int VScintillaBase::lineFoldLevel(int line)
{
    return this->SendScintilla(QsciScintilla::SCI_GETFOLDLEVEL, line) & QsciScintilla::SC_FOLDLEVELNUMBERMASK;
}

int VScintillaBase::lineStartPos(int line)
{
    return this->SendScintilla(QsciScintilla::SCI_POSITIONFROMLINE, line);
}

int VScintillaBase::lineEndPos(int line)
{
    return this->SendScintilla(QsciScintilla::SCI_GETLINEENDPOSITION, line);
}

int VScintillaBase::positionFormPoint(int x, int y)
{
    return this->SendScintilla(QsciScintilla::SCI_POSITIONFROMPOINT, x, y);
}

int VScintillaBase::posToLineQStringPos(int pos)
{
    int line = posOfLine(pos);
    int line_start = lineStartPos(line);
    if (line_start == pos)
    {
        return 0;
    }

    QString line_text = this->text(line);
    int temp_pos = line_start;
    for (int i = 0; i < line_text.length(); i++)
    {
        QString str = line_text.at(i);
        temp_pos += str.toStdString().length();
        if (temp_pos == pos)
        {
            return i + 1;
        }
    }

    return -1;
}

void VScintillaBase::setSelect(int start, int end)
{
    this->SendScintilla(QsciScintilla::SCI_SETSEL, start, end);
}

void VScintillaBase::setSelectStartPos(int pos)
{
    this->SendScintilla(QsciScintilla::SCI_SETSELECTIONSTART, pos);
}

void VScintillaBase::setSelectEndPos(int pos)
{
    this->SendScintilla(QsciScintilla::SCI_SETSELECTIONEND, pos);
}

void VScintillaBase::setCursorPos(int pos)
{
    this->SendScintilla(QsciScintilla::SCI_SETCURRENTPOS, pos);
}

void VScintillaBase::setAnchorPos(int pos)
{
    this->SendScintilla(QsciScintilla::SCI_SETANCHOR, pos);
}

void VScintillaBase::setLineFold(int line)
{
    this->setLineFold(line);
//    this->SendScintilla(QsciScintilla::SCI_TOGGLEFOLDSHOWTEXT, line, (char*)"...}");
}

void VScintillaBase::insertText(int pos, const char * text)
{
    this->SendScintilla(QsciScintilla::SCI_INSERTTEXT, pos, (int)text);
}

void VScintillaBase::deleteRange(int pos, int length)
{
    this->SendScintilla(QsciScintilla::SCI_DELETERANGE, pos, length);
}

void VScintillaBase::backTab()
{
    this->SendScintilla(QsciScintilla::SCI_BACKTAB);
}

void VScintillaBase::Tab()
{
    this->SendScintilla(QsciScintilla::SCI_TAB);
}

void VScintillaBase::clear()
{
    this->SendScintilla(QsciScintilla::SCI_CLEAR);
}

void VScintillaBase::addText(QString text)
{
    this->SendScintilla(QsciScintilla::SCI_ADDTEXT, text.toStdString().length(), text.toStdString().c_str());
}

bool VScintillaBase::getFoldExpanded(int line)
{
    auto result = this->SendScintilla(QsciScintilla::SCI_GETFOLDEXPANDED, line);
    if (result == 0)
    {
        return false;
    }
    return true;

}

